package websocket.chat;

import io.netty.channel.Channel;
import io.netty.channel.group.ChannelGroup;
import io.netty.channel.group.DefaultChannelGroup;
import io.netty.util.concurrent.GlobalEventExecutor;

import java.util.concurrent.ConcurrentHashMap;

public class ChannelConfigs {
    /**
     * 定义一个channel组，管理所有的channel * GlobalEventExecutor.INSTANCE 是全局的事件执行器，是一个单例
     */
    private  static ChannelGroup channelGroup;

    /**
     * 存放用户与Chanel的对应信息，用于给指定用户发送消息
     */
    private static ConcurrentHashMap<String, Channel> userChannelMap = new ConcurrentHashMap<>();

    private static Object object = new Object();
    /**
     * 获取channel组
     */
    public static ChannelGroup getChannelGroup() {
        if(channelGroup==null){
            synchronized (object){
                if(channelGroup==null){
                    channelGroup = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);
                    return channelGroup;
                }else{
                    return channelGroup;
                }
            }
        }else{
            return channelGroup;
        }
    }

    /**
     * 获取用户channel map
     */
    public static ConcurrentHashMap<String, Channel> getUserChannelMap() {
        return userChannelMap;
    }
}